package pers.acp.test.application.conf

import com.mybatisflex.core.FlexGlobalConfig
import com.mybatisflex.core.keygen.IKeyGenerator
import com.mybatisflex.core.keygen.KeyGeneratorFactory
import com.mybatisflex.solon.MyBatisFlexCustomizer
import io.gitee.zhangbinhub.acp.boot.log.LogAdapter
import io.gitee.zhangbinhub.acp.core.common.CommonTools
import org.noear.solon.annotation.Configuration
import org.noear.solon.annotation.Inject

/**
 * 自定义Jackson配置
 */
@Configuration
class MybatisFlexConfiguration(@param:Inject private var logAdapter: LogAdapter) : MyBatisFlexCustomizer {
    class AcpUUIDKeyGenerator : IKeyGenerator {
        override fun generate(entity: Any, keyColumn: String): Any {
            return CommonTools.getUuid()
        }
    }

    override fun customize(flexGlobalConfig: FlexGlobalConfig) {
        KeyGeneratorFactory.register("AcpUUID", AcpUUIDKeyGenerator())
//        val enabledSqlLog = Solon.cfg().getBool("solon.dataSources.sql.log", false)
//        if (enabledSqlLog) {
//            AuditManager.setAuditEnable(true)
//            AuditManager.setMessageCollector { auditMessage ->
//                StringBuilder("execute sql:\n")
//                    .append("| dsName | ${auditMessage.dsName}\n")
//                    .append("|    sql | ${auditMessage.fullSql}\n")
//                    .append("| params | ${auditMessage.queryParams}\n")
//                    .append("|  count | ${auditMessage.queryCount}\n")
//                    .append("|   time | ${auditMessage.elapsedTime} ms")
//                    .apply {
//                        logAdapter.info(this.toString())
//                    }
//            }
//        }
    }
}